<?php
/**
 * 好大一个测试，绿色的祝福
 * @author wangtao
 * @version 1.0
 */

require 'include.php';
#require 'config.php';

define('HOST', '127.0.0.1');
define('USER', 'root');
define('PASS', 'tto123');
define('DB', 'feed');


#初始化数据库
//$link = wt_SQL::connect(HOST,USER,PASS,DB);
/*
20100607
*/

$sql = 'SELECT `Blog_Feed` FROM blog_info WHERE `Blog_State` =1';
#得到源列表
$blogRss = get_one_arr($sql);
$RSS = array();
foreach ($blogRss as $feed)
{
	
	#获得所有地址列表
	#selectPostLink($feed);

	#获得所有源的文章列表
	$RSS = wt_xml::readRSS($feed);

	foreach ($RSS['post'] as $v)
	{
		if (strtotime($v['pubDate']) > getLastUpdate($feed))
		{
			addPost($v);
		}
		else
		{
			break;
		}	
	}
	//exit;
}
//echo 'RSS post v link IF ' . "\n";


#加入文章
function addPost($p)
{
	#初始化数据库
	try {
		$phpdb = new PDO('mysql:host=' . HOST . ';dbname=' . DB, USER, PASS);
	}
	catch (PDOException $e)
	{
		die ('Error!: ' . $e->getMessage() . '<br/>');
	}
	#开始存入数据
	
	$postCreator = empty($p['dc_creator']) ? empty($p['author']) ? wt_xml::getDomain($p['feed']) : $p['author'] : $p['dc_creator'];
	$postContent = empty($p['content_encoded']) ? $p['description'] : $p['content_encoded'];
	$slashComments = empty($p['slash_comments']) ? 0 : $p['slash_comments'];
	$sql = 'INSERT INTO `feed`.`post_into` (`Pid` ,`Post_Title` ,`Post_Link` ,`Post_PubDate` ,`Post_Creator` ,`Post_Content` ,`Post_Comments` ,`Post_State` ,`Post_Feed`) VALUES (NULL ,\'' . addslashes($p['title']) . '\',\'' . addslashes($p['link']) . '\',\'' . $p['pubDate'] . '\',\'' . addslashes($postCreator) . '\',\'' . addslashes($postContent) . '\',\'' . $slashComments . '\',' . '0' . ',\'' . addslashes($p['feed']) . '\')';
	$sql2 = 'UPDATE `feed`.`blog_info` SET `Blog_LastBuildDate` = \'' . $p['pubDate'] . '\' WHERE `blog_info`.`Blog_Feed` = \'' . addslashes($p['feed']) . '\';';
	//echo $sql . "\n";
	try {
		$phpdb -> exec($sql);
		$phpdb -> exec($sql2);
		$phpdb -> commit();
		$strPosr = $p['title'] . ' => is OK. | link => ' . $p['link'] . ' | Time:' . date('Y-m-d H:i:s') . "\n";
		Log::writeLog($strPosr);
		}
	catch (Exception $e) {
		//$phpdb -> rollBack();
		$strPosr = $p['title'] . ' => is OK. | link => ' . $p['link'] . ' | Time:' . date('Y-m-d H:i:s') . "\n";
		Log::writeLog($strPosr);
		Log::writeLog("Failed: " . $e->getMessage() . ' | Time:' . date('Y-m-d H:i:s') . "\n");
		}
	//echo '更新成功';
}


/**
 * 写入日志函数
 */
function writeLog($str)
{
	$file = dirname(__FILE__) . '/Log/' . date('Y-m-d') . '.txt';
	if (file_exists($file))
	{
		if (is_writable($file))
		{
			if (!$handle = fopen($file, 'a'))
			{
				echo '不能打开文件' . $file;
				return false;
			}
			if (fwrite($handle, $str) === false)
			{
				echo '不能写入到文件' . $file;
				return false;
			}
			fclose($handle);
		}
		else
		{
		
			echo '说好的文件：' . $file;
		}
	}
	else
	{
		fopen($file, "w+");
	}
}

/*
 * 得到文章地址列表
 */
function selectPostLink($feed)
{
	$postLinkSQL = 'SELECT  `Post_Link` FROM  `post_into` WHERE  `Post_Feed` =  \'' . $feed . '\'';
	return get_one_arr($postLinkSQL);
}

/**
 * 得到最后更新时间
 * @param string $feed 源
 */
function getLastUpdate($feed)
{
	$sql = 'SELECT `Blog_LastBuildDate` FROM `blog_info` WHERE 1 AND `Blog_Feed` = \'' . $feed . '\' LIMIT 0 , 1';
	$array = get_one_arr($sql);
	return strtotime($array[0]);
}

function getLastUpdateSTR($feed)
{
	$sql = 'SELECT `Post_PubDate` FROM `post_into` WHERE 1 AND `Post_Feed` = \'' . $feed . '\' ORDER BY `post_into`.`Pid` DESC LIMIT 0 , 1';
	//echo $sql;exit;
	$array = get_one_arr($sql);
	return $array[0];
	//wt_SQL::get_str($sql);
}

/**
 * 更新源最后更新时间
 * @param string $feed 源
 * @param string $time 时间
 */
function uupdateTime($feed, $time)
{
	$sql = 'UPDATE `feed`.`blog_info` SET `Blog_LastBuildDate` = \'' . $time . '\' WHERE `blog_info`.`Blog_Feed` = \'' . $feed . '\'';
	exeSQL($sql);
}

/**
 * 返回数组
 *
 */
function get_one_arr($sql)
{
	#初始化数据库
	try {
		$phpdb = new PDO('mysql:host=' . HOST . ';dbname=' . DB, USER, PASS);
	}
	catch (PDOException $e)
	{
		die ('Error!: ' . $e->getMessage() . '<br/>');
	}
	
	$sth = $phpdb -> prepare($sql);
	$sth -> execute();
	//返回一列
	return $result = $sth -> fetchAll(PDO::FETCH_COLUMN, 0);
}
/**
 * 执行SQL语句
 * @param unknown_type $sql
 */
function exeSQL($sql)
{
	#初始化数据库
	try {
		$phpdb = new PDO('mysql:host=' . HOST . ';dbname=' . DB, USER, PASS);
	}
	catch (PDOException $e)
	{
		die ('Error!: ' . $e->getMessage() . '<br/>');
	}
	
	$sth = $phpdb -> exec($sql);
}